// view/index/addpublics/addpublics.js
let app = getApp()
let yuming = app.globalData.yuming
let reg = app.globalData.mobile
var chaninese = app.globalData.chaninese
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    buttonClicked: false,
    yuming: yuming,
    heights: "",
    title: '场所消杀',
    photo: [],
    wuphoto: '',
    id: '',
    leilist: [{
      name: '植物油',
      id: 1
    }, {
      name: '制糖',
      id: 2
    }], //类别
    mcglrylist: [],
    xdcsList: [],
    mcglryindex: 0,
    mcglryid: '',
    mcglryguo: '',
    mchfs: '', //灭虫害方式
    mcypjx: '', //灭虫药品机械
    shiguo: '请选择使用时间', //灭虫害时间
    bindmchsymd: '', //使用目的
    bindmcsyqy: '', //使用区域
    bindmcpznd: '', //配置浓度
    bindmcbuytime: '请选择购买时间',
    bindtjjfwyl: '', //范围用量
    page: 1,
    scnum: 9,
    sdxdsl: 1,
    xiaodu: '请选择消毒方式',
    xiaoduindex: 0,
    xiaodulist: ['喷洒', '紫外线'],
    disRemark: '', //备注
    yaopin: '请选择产品', //药品
    yaopinindex: 0,
    yaopinlist: ['酒精', '84消毒液'],
    xiaosha: '请选择消杀人员', //消杀人员
    nongdu: '请选择浓度',
    nongduindex: 0,
    nongdulist: ['70%-75%', '1:10', '1:80'],
    xiaoshalist: [],
    jinghe_id:''
  },
  // 备注
  disRemark(e) {
    this.setData({
      disRemark: e.detail.value
    })
  },
  // 放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  //多选
  userCheck: function (e) {
    let index = e.currentTarget.dataset.id; //获取用户当前选中的索引值
    let Box = this.data.xdcsList
    if (Box[index].checked) {
      this.data.xdcsList[index].checked = false;
    } else {
      this.data.xdcsList[index].checked = true;
    }
    this.setData({
      xdcsList: this.data.xdcsList
    })
    // 返回用户选中的值
    let value = Box.filter((item, index) => {
      return item.checked == true;
    })
    let xditem = ''
    value.forEach((item, index) => {
      xditem += item.value + ','
    })
    xditem = xditem.substring(0, xditem.lastIndexOf(','));
    this.setData({
      mcglryguo: xditem
    })
    console.log(this.data.mcglryguo)
  },
  // 提交
  tijiao() {
    // if (this.data.mcglryguo == '') {
    //   wx.showToast({
    //     title: '请选择消毒场所',
    //     icon: 'none'
    //   })
    // } else if (this.data.xiaodu == '请选择消毒方式') {
    //   wx.showToast({
    //     title: '请选择消毒方式',
    //     icon: 'none'
    //   })
    // } else if (this.data.yaopin == '请选择产品') {
    //   wx.showToast({
    //     title: '请选择产品',
    //     icon: 'none'
    //   })
    // } else if (this.data.nongdu == '请选择浓度') {
    //   wx.showToast({
    //     title: '请选择浓度',
    //     icon: 'none'
    //   })
    // } else if (this.data.xiaosha == '请选择消杀人员') {
    //   wx.showToast({
    //     title: '请选择消杀人员',
    //     icon: 'none'
    //   })
    // } else if (this.data.disRemark == '') {
    //   wx.showToast({
    //     title: '请输入备注',
    //     icon: 'none'
    //   })
    // } else
     if (this.data.photo.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
    } else {
      this.mcgljk()
    }
  },

  // 配置浓度
  bindnongdu(e) {
    this.setData({
      nongdu: this.data.nongdulist[e.detail.value],
    })
  },
  // 消毒场所
  bindmcglry(e) {
    this.setData({
      mcglryguo: this.data.mcglrylist[e.detail.value],
    })
  },
  // 消毒方式
  bindxiaodu(e) {
    this.setData({
      xiaodu: this.data.xiaodulist[e.detail.value],
    })
  },
  // 使用药品
  bindyaopin(e) {
    this.setData({
      yaopin: this.data.yaopinlist[e.detail.value],
    })
  },
  // 消杀人员
  bindxiaosha(e) {
    this.setData({
      xiaosha: this.data.xiaoshalist[e.detail.value].Name,
    })
  },
  // 消毒场所
  mcrylb() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Besall/cartrelationInfo', {
      // page: that.data.page,
      id: wx.getStorageSync('stryId')
    }, "POST").then(res => {
      wx.hideLoading()
      console.log(res)
      this.setData({
        mcglrylist: res.data.data.fangyi_room,
      })
      let checkBox = this.data.mcglrylist;
      checkBox.forEach(item => {
        let str = {
          'value': item
        }
        this.data.xdcsList.push(str)
      })
      this.setData({
        xdcsList: this.data.xdcsList
      })
    })
  },
  //  接口
  mcgljk() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'shop/shopAddShopKilllog', {
      shopId: wx.getStorageSync('shopId'),
      disSite: that.data.mcglryguo,
      disMethod: that.data.xiaodu,
      useofDrugs: that.data.yaopin,
      drugCont: that.data.nongdu,
      disPeple: that.data.xiaosha,
      disImages: that.data.wuphoto,
      disRemark: that.data.disRemark
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var dd = wuphoto.split(",")
    photo.splice(index, 1)
    dd.splice(index, 1)
    var rr = dd.join(",")
    this.setData({
      photo: photo,
      wuphoto: rr
    })
  },
  // 上传
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i],1).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },

  // 生成二维码
  sheng() {
    wx.navigateTo({
      url: '/my_other/xinxilr/xinxilr',
    })
  },

  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  // 列表
  cylist() {
    var that = this
    this.myRequest(yuming + 'shop/shopSelectStaff', {
      page: that.data.page,
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      that.setData({
        xiaoshalist: res.data.result,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      jinghe_id:wx.getStorageSync('jinghe_id')
    })
    this.mcrylb();
    // 从业人员列表
    this.cylist();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})